/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import etcore.webapp.grouptalk.common.dao.BaseDAO;
import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;
import etcore.webapp.grouptalk.common.service.BaseService;
import etcore.webapp.grouptalk.user.dao.UserDAO;
import etcore.webapp.grouptalk.user.object.User;

/**
 * UserService implements queries and transactions on User
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class UserService extends BaseService {
	@Override
	public void deleteSingle(Object persistentInstance) {

		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		try {
			BaseDAO dao = new UserDAO();
			dao.deleteSingle(persistentInstance);
			tx.commit();

		} catch (RuntimeException re) {
			re.printStackTrace();
			tx.rollback();
		} finally {
			if (session != null)
				session.close();
		}
	}

	public User findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			User result = dao.findById(id);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public User findByUserName(String uname) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			User result = dao.findByUserName(uname);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long verify(String uname, String pwd) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			long result = dao.verify(uname, pwd);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<User> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			List<User> result = dao.findByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<User> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			List<User> result = dao.findByProperty(propertyName, value, begin,
					length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			long result = dao.findSizeByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<User> findAll() {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			List<User> result = dao.findAll();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<User> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			List<User> result = dao.findAll(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<User> findUsers(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			List<User> result = dao.findUsers(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<User> findAllBySort(int begin, int length, String sort) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			List<User> result = dao.findAllBySort(begin, length, sort);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<User> findByFutureDays(String inputDate, Integer days,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			List<User> result = dao.findByFutureDays(inputDate, days, begin,
					length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeByFutureDays(String inputDate, Integer days) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			long result = dao.findSizeByFutureDays(inputDate, days);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeByFutureDays(Integer days) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			long result = dao.findSizeByFutureDays(days);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findAllSize() {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			long result = dao.findAllSize();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSize() {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			long result = dao.findSize();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long searchSizeByProperty(String propertyName, String value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			long result = dao.searchSizeByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<User> searchByProperty(String propertyName, String value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			List<User> result = dao.searchByProperty(propertyName, value,
					begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<User> searchByPropertyBySort(String propertyName, String value,
			int begin, int length, String sort) {
		Session session = HibernateSessionFactory.getSession();
		try {
			UserDAO dao = new UserDAO();
			List<User> result = dao.searchByPropertyBySort(propertyName, value,
					begin, length, sort);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

}